package com.xzy.controller;

import com.xzy.config.Audience;
import com.xzy.entity.Tech;
import com.xzy.service.TechService;
import com.xzy.utils.FileUtils;
import com.xzy.utils.JSONResult;
import com.xzy.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@ResponseBody
public class TechController {
   @Autowired
    private TechService techService;
   @Autowired
    private Audience audience;
   @PostMapping("/question/tech/add")
    public JSONResult addTech(Tech tech, HttpServletRequest request)
   {
       String token = request.getHeader("Authorization");
       String _userId= JWTUtils.getUserId(token,audience.getBase64Secret());
       int userId=Integer.parseInt(_userId);
       tech.setUserId(userId);
       String url= FileUtils.getUrl(tech.getTechContent(),userId);
       tech.setTechContent(url);
       techService.addTech(tech);
       String content=FileUtils.getContent(url);
       tech.setTechContent(content);
       return JSONResult.SUCCESS(tech);
   }
   @GetMapping("/question/tech/getTech/{techId}")
    public JSONResult getTech(@PathVariable("techId") int techId)
   {
       Tech tech=techService.getTech(techId);
       String techContent=FileUtils.getContent(tech.getTechContent());
       tech.setTechContent(techContent);
       return JSONResult.SUCCESS(tech);
   }

   @GetMapping("/question/tech/findAll")
    public JSONResult findAll()
   {
       List<Tech> techList=techService.findAll();
       for(Tech tech:techList)
       {
           String content=FileUtils.getContent(tech.getTechContent());
           tech.setTechContent(content);
       }
       return JSONResult.SUCCESS(techList);
   }
    @PostMapping("/question/tech/getTechByType")
    public JSONResult getTechByType(@RequestParam("techType") String techType) {
        List<Tech> techList=techService.getTechByType(techType);
        for(Tech tech:techList)
        {
            String content=FileUtils.getContent(tech.getTechContent());
            tech.setTechContent(content);
        }
        return JSONResult.SUCCESS(techList);
    }
    @PutMapping("/question/tech/update/{techId}")
    public JSONResult updateTech(Tech tech,@PathVariable("techId") int techId)
    {
        Tech o_Tech=techService.getTech(techId);
        tech.setTechId(o_Tech.getTechId());
        tech.setUserId(o_Tech.getUserId());
        String url=FileUtils.getUrl(tech.getTechContent(),tech.getUserId());
        tech.setTechContent(url);
        techService.updateTech(tech);
        String content=FileUtils.getContent(url);
        tech.setTechContent(content);
        return JSONResult.SUCCESS(tech);
    }

    @DeleteMapping("/question/tech/deleteTech/{techId}")
    public JSONResult deleteTech(@PathVariable("techId")int techId)
    {
        Tech tech=techService.getTech(techId);
        techService.deleteTech(techId);
        return JSONResult.SUCCESS(tech);
    }

}
